package com.ly.controller;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ly.pojo.Result;
import com.ly.utils.aliOssUtil;

@RestController
public class FileUploadController {


    // 上传图片本地磁盘
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws IOException{
        // 把文件的内容存储到本地磁盘上
        String originalFilename = file.getOriginalFilename();
        // 保证文件的名字是唯一的，从而防止文件覆盖
        String filename = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
        file.transferTo(new File("C:\\Users\\pc\\Desktop\\图片和资源\\"+filename));
        return Result.success("url访问地址");
    }

    // 上传图片阿里云
    @PostMapping("/aliupload")
    public Result<String> aliupload(MultipartFile file) throws IOException, Exception{
        // 把文件的内容存储到本地磁盘上
        String originalFilename = file.getOriginalFilename();
        // 保证文件的名字是唯一的，从而防止文件覆盖
        String filename = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
        String url = aliOssUtil.uploadFile(filename,file.getInputStream());
        return Result.success(url);
    }
}
